package san.web.m;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import san.entity.m.Variety;
import san.service.m.VarietyService;
import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

@ParentPackage("default")
@Results( { @Result(name = "reload", location = "variety.shtml", type = "redirect") })
public class VarietyAction extends CRUDSupportAction {
	/**
	 * 自动生成VarietyAction类.
	 */
	private static final long serialVersionUID = 6651205543005990412L;
	@Autowired
	private VarietyService varietyService;
	private Page<Variety> page = new Page<Variety>(10);//每页10条记录
	private Variety variety;
	
	@Override
	protected void prepareModel() throws Exception {
		if (variety != null && StringUtils.isNotBlank(variety.getId())) {
			variety = varietyService.get(variety.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				variety = varietyService.get(getId());
			} else {
				variety = new Variety();
			}
		}
	}
	
	@Override
	public String delete() throws Exception {
		if (StringUtils.isNotBlank(getId())) {
			varietyService.delete(getId());
		}
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		return "input";
	}

	@Override
	public String list() throws Exception {
		setPage(varietyService.getAll(page));
		return SUCCESS;
	}

	@SuppressWarnings("finally")
	@Override
	public String save() throws Exception {
		try {
			varietyService.save(variety);
			addActionMessage("保存成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存失败!");
		}finally{
			return RELOAD;
		}
	}

	public Page<Variety> getPage() {
		return page;
	}

	public void setPage(Page<Variety> page) {
		this.page = page;
	}

	public Variety getVariety() {
		return variety;
	}

	public void setVariety(Variety variety) {
		this.variety = variety;
	}

}
